﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using MPP.Product.DataEntity;

namespace MPP.Product.DataManage
{
     public class FunctionItemTypeManage
    {
         public FunctionItemType InsertFunctionItemType(FunctionItemType functionItemType)
         {
            string strConn = SqlHelper.OracleMainDBConnectionString;
            using (OracleConnection oracleConn = new OracleConnection(strConn))
            {
                oracleConn.Open();

                int id = SqlHelper.GetNextID("FunctionItemType", "FUNCTIONITEMTYPEID");

                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into FUNCTIONITEMTYPE(FUNCTIONITEMTYPEID,NAME,LASTMODIFIEDTIME ,ISDELETED ,VERSION)");
                strSql.Append(" values (@FUNCTIONITEMTYPEID, @NAME,@LASTMODIFIEDTIME,@ISDELETED,@VERSION)");

                OracleParameter[] parameters = new OracleParameter[]
               {
                  new OracleParameter("@FUNCTIONITEMTYPEID",OracleType.Int32,40),
                  new OracleParameter("@NAME",OracleType.VarChar,50),
                  new OracleParameter("@LASTMODIFIEDTIME",OracleType.DateTime,40),
                  new OracleParameter("@ISDELETED",OracleType.Char,1),
                  new OracleParameter("@VERSION",OracleType.VarChar,40),
                };

                parameters[0].Value = id;
                parameters[1].Value = functionItemType.Name;
                parameters[2].Value = functionItemType.LastModifiedTime;
                parameters[3].Value = functionItemType.IsDeleted;
                parameters[4].Value = functionItemType.Version;

                OracleCommand oracleCommand = new OracleCommand(strSql.ToString(), oracleConn);
                int returndata = oracleCommand.ExecuteNonQuery();
                if (returndata > 0)
                {
                    StringBuilder strSqlSelect = new StringBuilder();
                    strSqlSelect.Append("select * from FUNCTIONITEMTYPE where FUNCTIONITEMTYPEID=@FUNCTIONITEMTYPEID");

                    OracleDataAdapter oracleAdapter = new OracleDataAdapter(strSqlSelect.ToString(), strConn);
                    DataTable datatable = new DataTable();
                    oracleAdapter.Fill(datatable);
                    oracleConn.Close();

                    FunctionItemType newFunctionItemType = new FunctionItemType();
                    if (datatable.Rows.Count > 0)
                    {
                        newFunctionItemType.FunctionItemTypeID = Convert.ToInt32(datatable.Rows[0][0]);
                        newFunctionItemType.Name = datatable.Rows[0][1].ToString();
                        newFunctionItemType.LastModifiedTime = Convert.ToDateTime(datatable.Rows[0][2]);
                        newFunctionItemType.IsDeleted = Convert.ToBoolean(datatable.Rows[0][3]);
                        newFunctionItemType.Version = Convert.ToInt32(datatable.Rows[0][4]);
                    }

                    return newFunctionItemType;
                }
            }
         }
    }
}
